<?php
namespace App\Http\Controllers\v1;

use Illuminate\Http\Request;
use Mockery\Exception;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use App\Http\Controllers\RequestBackController;
use App\Http\Controllers\Controller;
use App\Http\Controllers\CheckController;

// ->whereRaw("FIND_IN_SET($menu_id,purview_personal)")
class EditController extends Controller{
    /*
    案件案情展示case_title标题 case_update_time最近保存时间 case_situation案源说明case_action_id案由id case_num案号
    */
    public function ShowCaseById($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['CaseId','UserId'])){//案件id
            return RequestBackController::Structure(502);
        }else{
			//print_r($pos['CaseId']);exit();
            $CaseInfo = DB::connection('mysql_other')->table('yl_case as c')->select('c.case_action_type_id as CaseActionTypeId','c.case_action_detail_id as CaseActionDetailId','c.case_end_time as CaseEndTime','c.case_member_id as CaseMemberId','c.case_id as CaseId','c.case_title as CaseTitle','c.case_class_id as CaseClassId','c.case_update_time as CaseUpdateTime','c.litigants_id as LitigantsId','c.case_situation as CaseSituation','c.case_action_id as CaseActionId','c.case_num as CaseNum','c.main_appeal as MainAppeal','c.case_focus as CaseFocus','c.case_from_id as CaseFromId','c.case_acceptance_id as CaseAcceptanceId','c.case_stage as CaseStage','c.case_executor_id as CaseExecutorId','ca.case_action_id as CaseActionId','ca.case_action_title as CaseActionTitle','cl.case_class_id as CaseClassId','cl.case_class_title as CaseClassTitle','a.case_acceptance_id as CaseAcceptanceId','a.case_acceptance_name as CaseAcceptanceName','a.case_acceptance_flow as CaseAcceptanceFlow','a.case_acceptance_executor as CaseAcceptanceExecutor','a.case_acceptance_contact as CaseAcceptanceContact')
                                                    ->join("yl_case_class as cl",function($join){
                                                        $join->on('c.case_class_id','=','cl.case_class_id');
                                                    })
                                                    ->join("yl_case_action as ca",function($join){
                                                        $join->on('c.case_action_id','=','ca.case_action_id');
                                                    })
                                                    ->join("yl_case_acceptance as a",function($join){
                                                        $join->on('c.case_acceptance_id','=','a.case_acceptance_id');
                                                    })
                                                    ->where('c.case_id',$pos['CaseId'])
                                                    ->get()->map(function ($val){
													$val = (array)$val;
													return $val;
													})
													->keyBy(function($item)
													{
														return $item['CaseId'];
													})
													->toArray();
             //print_r($CaseInfo);exit();
           if($CaseInfo){
                $UserInfo = DB::connection('mysql_other')->table('yl_user as u')->select('u.user_id as UserId','u.user_name as UserName','u.user_login as UserLogin','u.user_img as UserImg')
                            ->get()->map(function ($val){
                                $val = (array)$val;
                                return $val;
                                })
                                ->keyBy(function($item)
                                {
                                    return $item['UserId'];
                                })
                                ->toArray();
                $LitigantInfo = DB::connection('mysql_other')->table('yl_litigants as l')->select('l.litigant_id as LitigantId','l.case_id as CaseId','l.litigation_status_id as LitigationStatusId','l.litigant_id_card as LitigantIdCard','l.litigant_name as LitigantName','l.litigant_mobile as LitigantMobile','s.litigation_status_id as LitigationStatusId','s.litigation_status_name as LitigationStatusName')
                                ->join("yl_litigation_status as s",function($join){
                                    $join->on('s.litigation_status_id','=','l.litigation_status_id');
                                    })
                                ->where('l.case_id',$pos['CaseId'])
                                ->get()->map(function ($val){
                                    $val = (array)$val;
                                    return $val;
                                    })
                                    ->keyBy(function($item)
                                    {
                                        return $item['LitigantId'];
                                    })
                                    ->toArray();
						//print_r($LitigantInfo);die;
				$mission = DB::connection('mysql_other')->table('yl_mission')->select('mission_id as MissionId','case_id as CaseId','mission_content as MissionContent','mission_count as MissionCount','mission_complete as MissionComplete','mission_end as MissionEnd')
						->where('case_id',$pos['CaseId'])
										->first();
				//print_r($mission);die;
                $row=array();
                foreach ($CaseInfo as $k => $v) {
					$row['MissionId'] = $mission->MissionId;
					$row['MissionContent'] = $mission->MissionContent;
					$row['MissionCount'] = $mission->MissionCount;
					$row['MissionComplete'] = $mission->MissionComplete;
					if($mission->MissionComplete == 1){
						$row['MissionEnd']="项目终止";
					}else{
						$row['MissionEnd']="项目未终止";
					}
                    $row['CaseId'] = $v['CaseId'];
                    $row['CaseTitle'] =$v['CaseTitle'];
                    $row['CaseClassId'] = $v['CaseClassId'];
                    $row['CaseSituation'] =$v['CaseSituation'];
                    $row['CaseActionId'] =$v['CaseActionId'];
                    $row['CaseNum'] = $v['CaseNum'];
                    $row['MainAppeal'] = $v['MainAppeal'];
                    $row['CaseFocus'] = $v['CaseFocus'];
                    $row['CaseFromId'] = $v['CaseFromId'];
                    $row['CaseMemberId'] = $v['CaseMemberId'];
                    $row['CaseAcceptanceId'] = $v['CaseAcceptanceId'];
                    $row['CaseStage'] = $v['CaseStage'];
                    $row['CaseExecutorId'] = $v['CaseExecutorId'];
                    $row['CaseActionTitle'] =$v['CaseActionTitle'] ;
                    $row['CaseClassTitle'] = $v['CaseClassTitle'];
                    $row['CaseAcceptanceName'] =$v['CaseAcceptanceName'] ;
                    $row['CaseAcceptanceFlow'] = $v['CaseAcceptanceFlow'];
                    $row['CaseAcceptanceExecutor'] = $v['CaseAcceptanceExecutor'];
                    $row['CaseAcceptanceContact'] = $v['CaseAcceptanceContact'];
                    $row['LitigantsId'] =$LitigantInfo;
                    $CaseInfo[$k]['CaseFromId']=explode(',',$v['CaseFromId']);
                    $CaseInfo[$k]['CaseMemberId']=explode(',',$v['CaseMemberId']);
                    $CaseInfo[$k]['CaseExecutorId']=explode(',',$v['CaseExecutorId']);

                    foreach($CaseInfo[$k]['CaseFromId'] as $k1=>$v1){
                        if(isset($UserInfo[$v1]))
                        $CaseInfo[$k]['CaseFromName'][$k1] = $UserInfo[$v1];
                    }
                    foreach($CaseInfo[$k]['CaseMemberId'] as $k1=>$v1){
                        if(isset($UserInfo[$v1]))
                        $CaseInfo[$k]['CaseMemberName'][$k1] = $UserInfo[$v1];
                    }
                    foreach($CaseInfo[$k]['CaseExecutorId'] as $k1=>$v1){
                        if(isset($UserInfo[$v1]))
                        $CaseInfo[$k]['CaseExecutorName'][$k1] = $UserInfo[$v1];
                    }
                    $row['CaseFromName'] =$CaseInfo[$k]['CaseFromName'];
                    $row['CaseMemberName'] =$CaseInfo[$k]['CaseMemberName'];
                    $row['CaseExecutorName'] =$CaseInfo[$k]['CaseExecutorName'];
                    if($v['CaseUpdateTime'] == null || $v['CaseUpdateTime'] == ""){
                        $row['CaseUpdateTime'] ="";
                    }else{
                        $row['CaseUpdateTime'] =date('Y-m-d',$v['CaseUpdateTime']);
                    }
                    if($v['CaseEndTime'] == null || $v['CaseEndTime'] == ""){
                        $row['CaseEndTime'] ="";
                    }else{
                        $row['CaseEndTime'] =date('Y-m-d',$v['CaseEndTime']);
                    }
                }
                return RequestBackController::Structure(501,$row);
            }else{
                return RequestBackController::Structure(504);
            }
        }

    }
	
	public function ShowSingleCase($request)
	{
		if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['CaseId'])){
            return RequestBackController::Structure(502);
        }
		$res=DB::table('yl_case')->where('case_id',$pos['CaseId'])->first();
		$res->case_create_time=date('Y-m-d',$res->case_create_time);
		return RequestBackController::Structure(101,$res);
	}
	
	public function LitigatsUpdate($request)
	{
		if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['CaseId','LitigationArr'])){
            return RequestBackController::Structure(502);
        }
		$Ids=null;
		foreach($pos['LitigationArr'] as $k=>$v)
		{
			$arr=[
				'case_id'=>isset($pos['CaseId'])?$pos['CaseId']:'',
				'litigation_status_id'=>isset($v['LitigationStatusId'])?$v['LitigationStatusId']:'',
				'litigant_name'=>isset($v['LitigantName'])?$v['LitigantName']:'',
				'litigant_mobile'=>isset($v['LitigantMobile'])?$v['LitigantMobile']:'',
				'litigant_address'=>isset($v['LitigantAddress'])?$v['LitigantAddress']:'',
				'contact_person'=>isset($v['ContactPerson'])?$v['ContactPerson']:'',
				'contact_person_mobile'=>isset($v['ContactPersonMobile'])?$v['ContactPersonMobile']:'',
				'litigant_id_card'=>isset($v['LitigantIdCard'])?$v['LitigantIdCard']:'',
				'litigant_type_id'=>isset($v['LitigantTypeId'])?$v['LitigantTypeId']:'',
				'lawsuit_id'=>isset($v['LitigantLawsuit'])?$v['LitigantLawsuit']:'' 				
			];
			if(isset($v['LitigantId']))
            {
				DB::table('yl_litigants')->where('litigant_id',$v['LitigantId'])->update($arr);
                $tempId=$v['LitigantId'];
            }
			else
            {
				$tempId=DB::table('yl_litigants')->insertGetId($arr);
            }
            if(empty($Ids))
            {
                $Ids=$tempId;
            }
            else
            {
                $Ids.=','.$tempId;
            }

		}
		return RequestBackController::Structure(101,$Ids);
	}
	public function LitigatsDelete($request)
	{
		if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['LitigantId'])){
            return RequestBackController::Structure(502);
        }
		DB::table('yl_litigants')->where('litigant_id',$pos['LitigantId'])->delete();
		return RequestBackController::Structure(101);
	}
	
	public function LitigatsList($request)
	{
		if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['CaseId'])){
            return RequestBackController::Structure(502);
        }
		$res=DB::table('yl_litigants')->select('litigant_id as LitigantId','case_id as CaseId','litigation_status_id as LitigationStatusId','litigant_name as LitigantName',
		'litigant_mobile as LitigantMobile','litigant_address as LitigantAddress','contact_person as ContactPerson',
		'contact_person_mobile as ContactPersonMobile','litigant_id_card as LitigantIdCard','litigant_type_id as LitigantTypeId','lawsuit_id as LawsuitId')
		->where('case_id',$pos['CaseId'])->get();
		return RequestBackController::Structure(101,$res);
	}
    /*
    案情编辑列表主体LitigationArr 当事人合集 CaseExecutorArr 承办律师合集
    */
    public function EditCase($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        $OurMap = array();
        if(!CheckController::CheckParameter($pos,['UserId','CaseId','LitigationArr','CaseNum','CaseTypeId','CaseActionId','CaseSituation','MainAppeal','CaseFocus','CaseAcceptanceId','CaseStage','CaseExecutorArr'])){
            return RequestBackController::Structure(502);
        }else{
            $Litigant = array();
            $map = array();
            foreach ($pos['LitigationArr'] as $k => $v) {//将传过来的当事人信息依次修改到表中
                    $Litigant['litigation_status_id'] = $v['LitigationStatusId'];
                    $Litigant['litigant_name'] = $v['LitigantName'];
                    $Litigant['litigant_mobile'] = $v['LitigantMobile'];
                    $UpLitigants = DB::connection('mysql_other')->table('yl_litigants')->where('litigant_id',$v['LitigantId'])->update($Litigant);
                }
            $map['case_type_id']=$pos['CaseTypeId'];
            $map['case_num']=$pos['CaseNum'];
            $map['case_action_id']=$pos['CaseActionId'];
            $map['case_situation']=$pos['CaseSituation'];
            $map['case_update_time']=time();
            $map['case_updater_id']=$pos['UserId'];
            $map['main_appeal']=$pos['MainAppeal'];
            $map['case_focus']=$pos['CaseFocus'];
            $map['case_stage']=$pos['CaseStage'];
            $map['case_executor_id']=$pos['CaseExecutorArr'];
            $UpCase = DB::connection('mysql_other')->table('yl_case')->where('case_id',$pos['CaseId'])->update($map);
            if($UpCase){
                return RequestBackController::Structure(601);
            }else{
                return RequestBackController::Structure(602);
            }

        }
    }
	 /*
    中止案件信息
    */
    public function StopCase($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['CaseId'])){//案件id
            return RequestBackController::Structure(502);
        }
        else{
            $Purview = new PurviewController();
            $UserPurview = $Purview->PurviewVerdict($request, $pos['UserId']);
            if ($UserPurview == 'All'){//只有管理员能中止项目
                $ApprovalInfo = DB::connection('mysql_other')->table('yl_approval')->where('case_id',$pos['CaseId'])->first();
                if(isset($ApprovalInfo)){//中止审批
                    $up=array();
                    $up['approval_is_end'] = 2;
                    $StoplApproval = DB::connection('mysql_other')->table('yl_case')->where('approval_id',$ApprovalInfo->approval_id)->update($up);
                }
                $missionInfo = DB::connection('mysql_other')->table('yl_mission')->where('case_id',$pos['CaseId'])->first();
                if (isset($missionInfo)){//中止任务
                    $update=array();
                    $update['mission_end'] = 1;
                    $StoplApproval = DB::connection('mysql_other')->table('yl_mission')->where('mission_id',$missionInfo->mission_id)->update($update);
                }
                return RequestBackController::Structure(601);
            }else{
                return RequestBackController::Structure(403);
            }

        }

    }

	/*
		保存设置
	*/
	public function updateSet($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['CaseId','CaseActionId','CaseActionSecondId','CaseActionDetailId','CaseMemberId','CaseFromId','CaseExecutorId'])){
            return RequestBackController::Structure(502);
        }else{
            $map = array();
            $map['case_action_id'] = isset($pos['CaseActionId'])&&!empty($pos['CaseActionId'])?$pos['CaseActionId']:null;//案由模板id
            $map['case_action_second_id']=isset($pos['CaseActionSecondId'])&&!empty($pos['CaseActionSecondId'])?$pos['CaseActionSecondId']:null;
            $map['case_action_detail_id'] = isset($pos['CaseActionDetailId'])&&!empty($pos['CaseActionDetailId'])?$pos['CaseActionDetailId']:null;
            $map['litigants_id'] = isset($pos['LitigantsId'])?$pos['LitigantsId']:'';//当事人集合
            $map['case_member_id'] = isset($pos['CaseMemberId'])?$pos['CaseMemberId']:'';//当事人集合
            $map['case_from_id'] = isset($pos['CaseFromId'])?$pos['CaseFromId']:'';//当事人集合
            $map['case_executor_id'] = isset($pos['CaseExecutorId'])?$pos['CaseExecutorId']:'';//当事人集合
			//print_r($map);die;
            $res=DB::table('yl_case')->where('case_id',$pos['CaseId'])->first();
            if($res){
                $upCase = DB::connection('mysql_other')->table('yl_case')->where('case_id',$pos['CaseId'])->update($map);
				//将细分模板的内容放入mission表中
				$findDetail = DB::connection('mysql_other')->table('yl_case_action_detail')->where('case_action_detail_id',$pos['CaseActionDetailId'])->first();
				$arr = array();
				$arr['case_id'] = $pos['CaseId'];
				$arr['mission_mode_id'] = $pos['CaseActionDetailId'];
				$arr['mission_content'] = $findDetail->case_action_detail_content;
				$findMission = DB::connection('mysql_other')->table('yl_mission')->where('case_id',$pos['CaseId'])->first();
				if(isset($findMission->case_id)){//任务表中存在案件
					if(isset($findMission->mission_mode_id) ==isset($pos['CaseActionDetailId'])){//不修改
						return RequestBackController::Structure(601);
					}else{
						$upMission = DB::connection('mysql_other')->table('yl_mission')->where('case_id',$pos['CaseId'])->update($arr);
						return RequestBackController::Structure(601);
					}
					
				}else{
					$inMission = DB::connection('mysql_other')->table('yl_mission')->insert($arr);
					return RequestBackController::Structure(601);
				}

            }else{
                return RequestBackController::Structure(505);
            }
        }
    }


    /*
       显示设置（权限设置未编写） CaseId 案件号
    */
    public function ShowSet($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['CaseId','CaseActionId','CaseActionSecondId','CaseActionDetailId'])){
            return RequestBackController::Structure(502);
        }else{
            $map = array();
            $map['case_action_id'] = isset($pos['CaseActionId'])?$pos['CaseActionId']:'';//案由模板id
            $map['case_action_second_id']=isset($pos['CaseActionSecondId'])?$pos['CaseActionSecondId']:'';//
            $map['case_action_detail_id'] = isset($pos['CaseActionDetailId'])?$pos['CaseActionDetailId']:'';//案由内容id
            $CaseInfo = DB::connection('mysql_other')->table('yl_case')->select('litigants_id as LitigantsId','case_id as CaseId','case_member_id as CaseMemberId','case_from_id as CaseFromId','case_executor_id as CaseExecutorId','purview_user as PurviewUser','message_user as MessageUser','manage_user as ManageUser')
                        ->where('case_id',$pos['CaseId'])
                        ->get()->map(function ($val){
                                    $val = (array)$val;
                                    return $val;
                                    })
                                    ->keyBy(function($item)
                                    {
                                        return $item['CaseId'];
                                    })
                        ->toArray();
                      
            $LitigantsInfo = DB::connection('mysql_other')->table('yl_litigants as l')->select('l.litigant_id as LitigantId','l.case_id as CaseId','l.litigation_status_id as LitigationStatusId','l.litigant_name as LitigantName','l.litigant_mobile as LitigantMobile')
                            ->where('l.case_id',$pos['CaseId'])
                            ->where('litigation_status_id',1)
                            ->get()
                            ->toArray();

            $UserInfo = DB::connection('mysql_other')->table('yl_user as u')->select('u.user_id as UserId','u.user_name as UserName','u.user_login as UserLogin','u.user_img as UserImg')
                        ->get()->map(function ($val){
                                    $val = (array)$val;
                                    return $val;
                                    })
                                    ->keyBy(function($item)
                                    {
                                        return $item['UserId'];
                                    })
                                    ->toArray();
                               //print_r($UserInfo);      print_r($CaseInfo); print_r($LitigantsInfo);die();
        if($CaseInfo){

            $UpdateCase=DB::connection('mysql_other')->table('yl_case')->where('case_id',$pos['CaseId'])->update($map);
             foreach ($CaseInfo as $k => $v) {
                    if($v['CaseExecutorId'] != null && !empty($v['CaseExecutorId'])){
                        $CaseInfo[$k]['CaseExecutorId']=explode(',',$v['CaseExecutorId']);
                        foreach ($CaseInfo[$k]['CaseExecutorId'] as $k1 => $v1) {
                            $CaseInfo[$k]['CaseExecutorId'][$k1] =isset($UserInfo[$v1])?$UserInfo[$v1]:'';
                        }
                    }
                    if($v['CaseMemberId'] != null && !empty($v['CaseMemberId'])){
                        $CaseInfo[$k]['CaseMemberId']=explode(',',$v['CaseMemberId']);
                        foreach ($CaseInfo[$k]['CaseMemberId'] as $k2 => $v2) {
                            $CaseInfo[$k]['CaseMemberId'][$k2] = isset($UserInfo[$v2])?$UserInfo[$v2]:'';
                        }
                    }
                    if($v['CaseFromId'] != null && !empty($v['CaseFromId'])){
                        $CaseInfo[$k]['CaseFromId']=explode(',',$v['CaseFromId']);
                        foreach ($CaseInfo[$k]['CaseFromId'] as $k3 => $v3) {
                            $CaseInfo[$k]['CaseFromId'][$k3] = isset($UserInfo[$v3])?$UserInfo[$v3]:'';
                        }
                    }
                    if($v['PurviewUser'] != null && !empty($v['PurviewUser'])){
                        $CaseInfo[$k]['PurviewUser']=explode(',',$v['PurviewUser']);
                        foreach ($CaseInfo[$k]['PurviewUser'] as $k4 => $v4) {
                            $CaseInfo[$k]['PurviewUser'][$k4] =isset($UserInfo[$v4])?$UserInfo[$v4]:'';
                        }
                    }
                    if($v['MessageUser'] != null && !empty($v['MessageUser'])){
                        $CaseInfo[$k]['MessageUser']=explode(',',$v['MessageUser']);
                        foreach ($CaseInfo[$k]['MessageUser'] as $k5 => $v5) {
                            $CaseInfo[$k]['MessageUser'][$k5] =isset($UserInfo[$v5])?$UserInfo[$v5]:'';
                        }
                    } 
                    if($v['ManageUser'] != null && !empty($v['ManageUser'])){
                        $CaseInfo[$k]['ManageUser']=explode(',',$v['ManageUser']);
                        foreach ($CaseInfo[$k]['ManageUser'] as $k6 => $v6) {
                            $CaseInfo[$k]['ManageUser'][$k6] = isset($UserInfo[$v6])?$UserInfo[$v6]:'';
                        }
                    }
                    $CaseInfo[$k]['Consignor'] = $LitigantsInfo;
                    
            }
            return RequestBackController::Structure(501,$CaseInfo);
        }else{
            return RequestBackController::Structure(505);
        }
    }
  }
        /*
        编辑设置  RelationId联络委托人集合未编写PurviewUser可见人员id集合 MessageUser可评论人的id集合 ManageUser可编辑用户id集合 //
        */
    public function EditSet($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['CaseId','RelationId','PurviewUser','MessageUser','ManageUser','CaseFromId','CaseMemberId','CaseExecutorId'])){
            return RequestBackController::Structure(502);
        }else{

            $inarr = array();
            $inarr['case_executor_id'] = $pos['CaseExecutorId'];
            $inarr['case_from_id'] = $pos['CaseFromId'];
            $inarr['case_member_id'] = $pos['CaseMemberId'];
            $inarr['purview_user'] =$pos['PurviewUser'];
            $inarr['message_user']=$pos['MessageUser'];
            $inarr['manage_user'] =$pos['ManageUser'];
            $MsgInfo = DB::connection('mysql_other')->table('yl_case')->where('case_id',$pos['CaseId'])->update($inarr);
            if($MsgInfo){
                return RequestBackController::Structure(601);
            }else{
                return RequestBackController::Structure(602);
            }


        }
    }


    /*
    删除案件信息
    */
    public function DelCase($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['CaseId'])){//案件id
            return RequestBackController::Structure(502);
        }
        else{
            $Purview = new PurviewController();
            $UserPurview = $Purview->PurviewVerdict($request, $pos['UserId']);
            if ($UserPurview == 'All'){//只有管理员能删除
                $CaseInfo = DB::connection('mysql_other')->table('yl_case')->where('case_id',$pos['CaseId'])->first();
                if($CaseInfo){//删除案件
                    $LitigantsId = $CaseInfo->litigants_id;//当事人字符串
					$LitigantsId=explode(',',$LitigantsId);
					//print_r($LitigantsId);die;
                    $DelCase = DB::connection('mysql_other')->table('yl_case')->where('case_id',$pos['CaseId'])->delete();
					if(isset($DelCase)){//删除当事人
						foreach ($LitigantsId as $k => $v) {
							$DelLitigant = DB::connection('mysql_other')->table('yl_litigants')->where('litigant_id',$v)->delete();
						}
					}
                    if(isset($DelLitigant)){
						   //删除收款
						$DelProceeds=DB::connection('mysql_other')->table('yl_proceeds')->where('case_id',$pos['CaseId'])->delete();
					}
                    if(isset($DelProceeds)){//删除案件任务
                         $DelMission =DB::connection('mysql_other')->table('yl_mission')->where('case_id',$pos['CaseId'])->delete();
					}
                    if(isset($DelMission)){//删除审批
                         $DelApproval = DB::connection('mysql_other')->table('yl_approval')->where('case_id',$pos['CaseId'])->delete();
                    }
                    return RequestBackController::Structure(601);
                        
                    
                }else{
					 return RequestBackController::Structure(505);
				}
            }else{
                return RequestBackController::Structure(403);
            }

        }

    }


    /*
    案件评论展示
    */
    public function ShowMessage($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['CaseId'])){//案件id
            return RequestBackController::Structure(502);
        }else{
            $MessageInfo = DB::connection('mysql_other')->table('yl_message as m')->select('m.message_id as MessageId','m.case_id as CaseId','m.message_remind as MessageRemind','m.message_content as MessageContent','m.message_creater_id as MessageCreaterId','m.message_created_time as MessageCreatedTime','u.user_id as UserId','u.user_name as UserName')
                        ->join("yl_user as u",function($join){
                            $join->on('m.message_creater_id','=','u.user_id');
                        })->where('m.case_id',$pos['CaseId'])->get()->map(function ($val){
                                    $val = (array)$val;
                                    return $val;
                                    })
                                    ->keyBy(function($item)
                                    {
                                        return $item['MessageId'];
                                    })->toArray();
            if($MessageInfo){
                foreach ($MessageInfo as $k => $v) {
                    $MessageInfo[$k]['MessageCreatedTime'] =date('H:s:i',$v['MessageCreatedTime']);
                    $MessageInfo[$k]['MessageCreated'] =date('Y-m-d',$v['MessageCreatedTime']);
                }
                //将数组进行分组再组合显示
                $MegInfo= array(); 
                foreach ($MessageInfo as $k1 => $v1){ 
                    $MegInfo[$v1['MessageCreated']][] = $v1; 
                }
            return RequestBackController::Structure(501,$MegInfo);
            }else{
                return RequestBackController::Structure(504);
            }        
        }
    }
    // public function test()
    // {
    //     $arr=array(
    //         1,2,3,4,5,6,7
    //     );
    //     print_r($arr[3]);
    // }

    /*
    当前案件的所有律师列表
    */
    public function CaseLawList($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
            if(!CheckController::CheckParameter($pos,['CaseId'])){//案件id
                return RequestBackController::Structure(502);
            }else{//查询当前这个案件中的所有律师人员
                $CaseInfo = DB::connection('mysql_other')->table('yl_case')->select('case_id as CaseId','case_member_id as CaseMemberId','case_from_id as CaseFromId','case_executor_id as CaseExecutorId')
                        ->where('case_id',$pos['CaseId'])
                        ->get()->map(function ($val){
                                    $val = (array)$val;
                                    return $val;
                                    })
                                    
                                    ->toArray();
                $UserInfo = DB::connection('mysql_other')->table('yl_user as u')->select('u.user_id as UserId','u.user_name as UserName','u.user_img as UserImg')
                        ->get()->map(function ($val){
                                    $val = (array)$val;
                                    return $val;
                                    })
                                    ->keyBy(function($item)
                                    {
                                        return $item['UserId'];
                                    })
                                    ->toArray();
                if($CaseInfo){
					$row =array();
						
                    foreach ($CaseInfo as $k => $v) {
						
                        $CaseInfo[$k]['CaseExecutorId']=explode(',',$v['CaseExecutorId']);
                        $CaseInfo[$k]['CaseMemberId']=explode(',',$v['CaseMemberId']);
                        $CaseInfo[$k]['CaseFromId']=explode(',',$v['CaseFromId']);
						if(isset($CaseInfo[$k]['CaseExecutorId'])){
							foreach ($CaseInfo[$k]['CaseExecutorId'] as $k1 => $v1) {
								$CaseInfo[$k]['CaseExecutorId'][$k1] = isset($UserInfo[$v1])?$UserInfo[$v1]:'';
							}
						}
                        if(isset($CaseInfo[$k]['CaseMemberId'])){
							foreach ($CaseInfo[$k]['CaseMemberId'] as $k1 => $v1) {
								$CaseInfo[$k]['CaseMemberId'][$k1] = isset($UserInfo[$v1])?$UserInfo[$v1]:'';
							}
						}
                        if(isset($CaseInfo[$k]['CaseFromId'])){
							foreach ($CaseInfo[$k]['CaseFromId'] as $k1 => $v1) {
								$CaseInfo[$k]['CaseFromId'][$k1] = isset($UserInfo[$v1])?$UserInfo[$v1]:'';
							}
						}
                        
						$row['CaseExecutorId'] = isset($CaseInfo[$k]['CaseExecutorId'])?$CaseInfo[$k]['CaseExecutorId']:'';
						$row['CaseMemberId'] = isset($CaseInfo[$k]['CaseMemberId'])?$CaseInfo[$k]['CaseMemberId']:'';
						$row['CaseFromId'] = isset($CaseInfo[$k]['CaseFromId'])?$CaseInfo[$k]['CaseFromId']:'';
						//$row['CaseId'] = $v['CaseId'];
                    }
                    return RequestBackController::Structure(501,$row);
                }else{

                    return RequestBackController::Structure(504);
                }
                
            }
    }
    /*
    单个案件添加评论(只是添加进数据库中未进行@人员的提示消息)
    */
    public function AddMsg($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['CaseId','MessageContent','MessageCreaterId'])){//案件id
            return RequestBackController::Structure(502);
        }else{//@对象的id集合
            if(CheckController::CheckParameter($pos,['CaseUser'])){
                $add = array();
                $add['message_remind'] = $pos['CaseUser'];
                $add['case_id'] =$pos['CaseId']; 
                $add['message_content'] = $pos['MessageContent'];
                $add['message_creater_id'] = $pos['MessageCreaterId'];
                $add['message_created_time'] = time();
                $MsgInfo = DB::connection('mysql_other')->table('yl_message')->insert($add);
                if($MsgInfo){
                    return RequestBackController::Structure(401);
                }else{
                    return RequestBackController::Structure(402);
                }
            }else{
                $add = array();
                $add['case_id'] =$pos['CaseId']; 
                $add['message_content'] = $pos['MessageContent'];
                $add['message_creater_id'] = $pos['MessageCreaterId'];
                $add['message_created_time'] = time();
                $MsgInfo = DB::connection('mysql_other')->table('yl_message')->insert($add);
                if($MsgInfo){
                    return RequestBackController::Structure(401);
                }else{
                    return RequestBackController::Structure(402);

                }
            }
        }
    }



















































   
}